package com.miplus.ccrm.core.service.cust;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.common.*;
import com.miplus.ccrm.core.controller.vo.conf.ConBlackAddReqVo;
import com.miplus.ccrm.core.controller.vo.conf.ConBlackDelReqVo;
import com.miplus.ccrm.core.controller.vo.conf.ConBlackPageReqVo;
import com.miplus.ccrm.core.controller.vo.conf.ConBlackPageResVo;
import com.miplus.ccrm.core.controller.vo.cust.ContResVo;
import com.miplus.ccrm.core.pojo.ConBlacklist;
import com.miplus.ccrm.core.pojo.ConBlacklistRemoveHist;
import com.miplus.ccrm.core.pojo.ConCustomer;
import com.miplus.ccrm.core.mapper.ConBlacklistMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author wangzl
* @description 针对表【con_blacklist】的数据库操作Service实现
* @createDate 2024-11-04 14:05:48
*/
@Service
public class ConBlacklistServiceImpl extends MPJBaseServiceImpl<ConBlacklistMapper, ConBlacklist>
    implements ConBlacklistService{


    @Resource
    private ConBlacklistRemoveHistService removeHistService;

    @Override
    public CmdResVo conBlackAddBatch(JSysUser user, ConBlackAddReqVo reqVo) {

        if(reqVo.getCustomernumber()==null && reqVo.getCustomernumber().size()==0){
            throw new ServiceException(EnumRCode.ArgErr.value, EnumRCode.ArgErr.name);
        }

        List<ConBlacklist> dbBlacklistList = selectJoinList(ConBlacklist.class,
                new MPJLambdaWrapper<>(ConBlacklist.class)
                        .in(ConBlacklist::getCustomernumber, reqVo.getCustomernumber())
//                        .eq(ConBlacklist::getType, reqVo.getType())
        );

        if(dbBlacklistList!=null
            && dbBlacklistList.size()>0){
            if(dbBlacklistList.get(0).getType()==1){
                throw new ServiceException(EnumRCode.CustomerBlack.value, EnumRCode.CustomerBlack.name);
            } else if (dbBlacklistList.get(0).getType()==2) {
                throw new ServiceException(EnumRCode.CustomerWhite.value, EnumRCode.CustomerWhite.name);
            }
        }

        List<ConBlacklist>blacklistList=new ArrayList<>();
        for (String customernumber : reqVo.getCustomernumber()) {
            ConBlacklist conBlacklist=new ConBlacklist();
            conBlacklist.setCustomernumber(customernumber);
            conBlacklist.setType(reqVo.getType());
            conBlacklist.setReason(reqVo.getReason());
            conBlacklist.setCrdt(new Date());
            conBlacklist.setCrusr(user.getUsrid());
            conBlacklist.setCrusrname(user.getName());
            blacklistList.add(conBlacklist);
        }
        saveBatch(blacklistList);

        return new CmdResVo(blacklistList.size(),"ok");
    }

    @Override
    public CmdResVo conBlackEdit(JSysUser user, ConBlackAddReqVo reqVo) {

        if(reqVo.getId()==null){
            throw new ServiceException(EnumRCode.ArgErr.value, EnumRCode.ArgErr.name);
        }

        ConBlacklist conBlacklist=new ConBlacklist();
        conBlacklist.setId(Long.valueOf(reqVo.getId()));
        conBlacklist.setType(reqVo.getType());
        conBlacklist.setReason(reqVo.getReason());
        conBlacklist.setMddt(new Date());
        conBlacklist.setMdusr(user.getUsrid());
        updateById(conBlacklist);

        return new CmdResVo(1,reqVo.getId());
    }

    @Override
    public IPage<ConBlackPageResVo> conBlackPage(JSysUser user, ConBlackPageReqVo reqVo) {


        String $usrid=user.getUsrid();
        String idstar= JUsrPerm.getPermission(user,JUsrPerm.DISSTAR_IDNO);

        if(reqVo.getIsRemove()){

            //展示移除数据
            return removeHistService.selectJoinListPage(new Page<>(reqVo.getPageNum(),reqVo.getPageSize()),
                            ConBlackPageResVo.class,
                            new MPJLambdaWrapper<>(ConBlacklistRemoveHist.class)
                                    .leftJoin(ConCustomer.class,"c",ConCustomer::getCustomernumber,ConBlacklistRemoveHist::getCustomernumber)
                                    .select(ConCustomer::getCustomernumber,ConCustomer::getCustname,
                                            ConCustomer::getMobile,ConCustomer::getPrododudtotalPrin,ConCustomer::getProdmthsStage,
                                            ConCustomer::getOverdueDays)
                                    .select(ConBlacklistRemoveHist::getRenotes,ConBlacklistRemoveHist::getReusrname)
                                    .select(ConBlacklistRemoveHist::getId,ConBlacklistRemoveHist::getReason,ConBlacklistRemoveHist::getType)
                                    .selectAs(ConBlacklistRemoveHist::getStarttime,ConBlackPageResVo::getCrdt)
                                    .selectAs(ConBlacklistRemoveHist::getEndtime,ConBlackPageResVo::getEndtime)
                                    .selectAs(ConBlacklistRemoveHist::getCrusrname,ConBlackPageResVo::getCrusrName)
                                    .selectAs(String.format("fi_starSecu('%s','%s',c.openacctcertno,'%s')",$usrid,JUsrPerm.DISSTAR_IDNO,idstar), ConBlackPageResVo::getOpenacctcertno)
                                    .selectAs(String.format("fi_starSecu('%s','disstar.phone',c.mobile,'')",$usrid), ConCustomer::getMobile)
                                    .like(JPubUtils.IsNotEmpty(reqVo.getCustname()),ConCustomer::getCustname,reqVo.getCustname())
                                    .like(JPubUtils.IsNotEmpty(reqVo.getOpenacctcertno()),ConCustomer::getOpenacctcertno,reqVo.getOpenacctcertno())
                                    .like(JPubUtils.IsNotEmpty(reqVo.getMobile()),ConCustomer::getMobile,reqVo.getMobile())
                                    .eq(JPubUtils.IsNotEmpty(reqVo.getType()),ConBlacklistRemoveHist::getType,reqVo.getType())

                    );

        }else{
            return selectJoinListPage(new Page<>(reqVo.getPageNum(),reqVo.getPageSize()),
                    ConBlackPageResVo.class,
                    new MPJLambdaWrapper<>(ConBlacklist.class)
                            .leftJoin(ConCustomer.class,"c",ConCustomer::getCustomernumber,ConBlacklist::getCustomernumber)
                            .selectAs(String.format("fi_starSecu('%s','%s',c.openacctcertno,'%s')",$usrid,JUsrPerm.DISSTAR_IDNO,idstar), ConBlackPageResVo::getOpenacctcertno)
                            .selectAs(String.format("fi_starSecu('%s','disstar.phone',c.mobile,'')",$usrid), ConCustomer::getMobile)
                            .select(ConCustomer::getCustomernumber,ConCustomer::getCustname,
                                    ConCustomer::getPrododudtotalPrin,ConCustomer::getProdmthsStage,
                                    ConCustomer::getOverdueDays)
                            .select(ConBlacklist::getId,ConBlacklist::getCrdt,ConBlacklist::getReason,ConBlacklist::getType)
                            .selectAs(ConBlacklist::getCrusrname,ConBlackPageResVo::getCrusrName)
                            .like(JPubUtils.IsNotEmpty(reqVo.getCustname()),ConCustomer::getCustname,reqVo.getCustname())
                            .like(JPubUtils.IsNotEmpty(reqVo.getOpenacctcertno()),ConCustomer::getOpenacctcertno,reqVo.getOpenacctcertno())
                            .like(JPubUtils.IsNotEmpty(reqVo.getMobile()),ConCustomer::getMobile,reqVo.getMobile())
                            .eq(JPubUtils.IsNotEmpty(reqVo.getType()),ConBlacklist::getType,reqVo.getType())
            );
        }
    }

    @Override
    public CmdResVo conBlackRemove(JSysUser user, ConBlackDelReqVo reqVo) {
        ConBlacklist dbBlacklist = getById(reqVo.getId());

        if(dbBlacklist==null){
            throw new ServiceException(EnumRCode.ArgErr.value, EnumRCode.ArgErr.name);
        }

        ConBlacklistRemoveHist conBlacklistRemoveHist=new ConBlacklistRemoveHist();
        conBlacklistRemoveHist.setCustomernumber(dbBlacklist.getCustomernumber());
        conBlacklistRemoveHist.setType(dbBlacklist.getType());
        conBlacklistRemoveHist.setStarttime(dbBlacklist.getCrdt());
        conBlacklistRemoveHist.setEndtime(new Date());
        conBlacklistRemoveHist.setReason(dbBlacklist.getReason());
        conBlacklistRemoveHist.setCrusr(dbBlacklist.getCrusr());
        conBlacklistRemoveHist.setCrusrname(dbBlacklist.getCrusrname());
        conBlacklistRemoveHist.setRecrusr(user.getUsrid());
        conBlacklistRemoveHist.setReusrname(user.getName());
        conBlacklistRemoveHist.setRenotes(reqVo.getRenotes());
        removeHistService.save(conBlacklistRemoveHist);
        boolean b = removeById(reqVo.getId());

        return new CmdResVo(1,conBlacklistRemoveHist.getId().toString());
    }
}




